package calculation;

/**
 *
 * @author KieuOanh
 */
public class Complex {

    double real;
    double imag;
    public final double eps = 1.0 / 5.0 + 1.0 / 5.0 - 1.0 / 10.0 - 1.0 / 10.0 - 1.0 / 10.0 - 1.0 / 10.0;

    public Complex(double real, double imag) {
        this.real = real;
        this.imag = imag;
    }

    Complex add(Complex otherComplex) {
        return new Complex(real + otherComplex.real, imag + otherComplex.imag);
    }

    Complex mul(Complex otherComplex) {
        return new Complex(real * otherComplex.real - imag * otherComplex.imag, real * otherComplex.imag + imag * otherComplex.real);
    }

    Complex div(Complex otherComplex) {
        return new Complex((real * otherComplex.real + imag * otherComplex.imag) / (pow2(otherComplex.real) + pow2(otherComplex.imag)+eps),
                (imag * otherComplex.real - real * otherComplex.imag) / (pow2(otherComplex.real) + pow2(otherComplex.imag)+eps));
    }

    Complex mul(double x) {
        return new Complex(real * x, imag * x);
    }

    Complex div(int x) {
        return new Complex(real / x, imag / x);
    }

    double module() {
        return Math.sqrt(real * real + imag * imag);
    }

    double pow2(double x) {
        return x * x;
    }
}
